import Vue from 'vue'
import Vuex from 'vuex'
import {
	process
} from '../common/api/process.js'
import {
	byteToString,
	stringToHex
} from "../common/api/bytetostring.js"
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		did: '',
		dic: '',
		key: "",
		key2: "",
		audioPath:'',
		countDownId:0,
		currentPostId: '', //当前操作职位id
	},
	mutations: {
		getKey(state) {
			let did = uni.getStorageSync('did')
			//console.log('did:',did);
			let dic = state.dic;
			let len = 16;
			if (!did || len <= 0) {
				return "";
			}
			try {
				let seed = stringToHex(did);
				let seedGroupSize = parseInt(seed.length / len);
				let dicGroupSize = parseInt(dic.length / len);
				let key = "";
				for (let l = 0; l < len; l++) {
					let sdg = seed.substring(l * seedGroupSize, (l + 1) * seedGroupSize);
					let index = parseInt(sdg, 16) % dicGroupSize;
					let start = dicGroupSize * l + index;
					key += dic.charAt(start);
				}
				let getKey = key
				//console.log('getkey:',getKey);
				state.key = getKey
				return key
			} catch (e) {
				console.log(e);
			}
			return "";
		},
		getKeys(state) {
			let did = uni.getStorageSync('did')
			let dic = state.dic;
			let len = 8;
			if (!did || len <= 0) {
				return "";
			}
			try {
				let seed = stringToHex(did);
				let seedGroupSize = parseInt(seed.length / len);
				let dicGroupSize = parseInt(dic.length / len);
				let key = "";
				for (let l = 0; l < len; l++) {
					let sdg = seed.substring(l * seedGroupSize, (l + 1) * seedGroupSize);
					let index = parseInt(sdg, 16) % dicGroupSize;
					let start = dicGroupSize * l + index;
					key += dic.charAt(start);
				}
				let getKey = key
				//console.log('getkey2',getKey);
				state.key2 = getKey
				return key
			} catch (e) {
				console.log(e);
			}
			return "";
		},
		getIndex(state){
			console.log(state.countDownId);
		}
		
		
	},
	actions: {
		initDic({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				if(state.key){
					resolve()
				}else{
					// var wxFile = wx.getFileSystemManager()
					// uni.downloadFile({
					// 	//正式环境密钥
					// 	// url: 'https://xuniuwang-global.oss-cn-hangzhou.aliyuncs.com/hotpot/food.png',
					// 	//测试环境密钥
					// 	url: 'https://static-e3f7196f-075c-4e60-9f8a-1256893d0377.bspapp.com/duck.png',	
					// 	header: {
					// 		"token": uni.getStorageSync("token")
					// 	},
					// 	success(res) {
					// 		let path = res.tempFilePath;
							
					// 		uni.getFileSystemManager().readFile({
					// 			filePath: path,
					// 			success: (result) => {
					// 				const arrayBuffer = result.data;
									
					// 				var bytes = process(arrayBuffer);
					// 				//state.dic = byteToString(bytes)	//将byte数组转为String
									
					// 				console.log('dic:',state.dic);
					// 				commit('getKey')
					// 				commit('getKeys')
					// 				resolve()
					// 			}
					// 		})
					// 	}
					// })
					
					// uni.setStorageSync('token','6b1f37b815330ec808d3189fa2cdc8e3')
					state.dic='wBe20QMheTuM1rMFr2GliBmWGXIGNbnK2Lu3hkSOrnOo9zCjrvYguAZ4bZ816eLyN6tI9TDVv8fepnB0IvQpJVSLsyZr7IoI6zvoZ0wScWtGt84g5EmtZV6oj726ZW2eZYmE7atfNzcOFKnsyDxCpAZmrVndShfKAhpxb2smIulcxSYwmFf9yDdKeC7jgm3NGRLdBSpyfalrzt2cBL1ISF2NpFIn3KmywOsITHDSLvr7lFNVkJcKQLL0ISciBrxTij2VNaNPNE5dfajY2BddUNDwE6ugb6NYxHQsgiwUJL2efPQR7grOqSUia8uGj1CuWwEowxa6XSJ5xzGutjdJvEAByC41nWTRdZCf0S47ZezSqpBZhhrf0Q4rF4XuVYwIKLpe1oh3SWGRXfGYtUXqN6qDvWsHnvwWyUyAaGcP1HOrxe2GwGo1oeJjifwbK3AAms96SwmHnZ84vEB2DWMjyuL6jPtQmOjN1cPLkETXA60sFvy5zTz4zHuYDbmyd1qypquHJQ9rUWFKtHaHZdlgBnefzjwOAL3sFCqidw7gF3VLvIQhqLTC7PjaFq0xWqMJjhFhVOryHb6lsGvxNNhR75Bk9wdsmagdhDzoEyYoVDz32a2mtIoW4x7Qe2jybhEkHdymGqU5vubvX6tRgDsdXAjDsC8eZKmPcKneGF2dGfpFWXiP9b6hSKPhbfyGFPOpvjK2p8SVaNx52a7E1DQ8PunYnCi6qw4MPJQ5u3u1XQktnmKYjQ8sk6KNZemh7ggN3UVgd7O2urrkIWZl4k4UxPYSwB2iQmoN'
					commit('getKey')
					commit('getKeys')
					resolve()
				}
				
			})
		},
		
		

	}
})
export default store
